package resources;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;

import src.Window;


/**
 * classe contem todas as ferramentas da Window
 * 
 * 
 * @author eklauberg
 *
 */
public class Ferramentas {
	
	private Window window;
	private Clipboard clipboard;
	private StringSelection selection;
	private Document document;
	
	public Ferramentas(Window window){
		this.window = window;
		this.document = new Document(this.window);
		clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
	}
	
    public void recortar() {
    	int selecaoInicio = window.getTxtCodigo().getSelectionStart();
    	int selecaoFinal = window.getTxtCodigo().getSelectionEnd();
		selection = new StringSelection(window.getTxtCodigo().getSelectedText());
		window.getTxtCodigo().replaceRange("", selecaoInicio, selecaoFinal);
		clipboard.setContents(selection, selection);
	}

	public void copiar() {
		selection = new StringSelection(window.getTxtCodigo().getSelectedText());
		clipboard.setContents(selection, selection);
	}

	public void colar() {		
		int selecaoInicio = window.getTxtCodigo().getSelectionStart();
    	int selecaoFinal = window.getTxtCodigo().getSelectionEnd();		
		Transferable t = clipboard.getContents(null);
		if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
			try {
				String data = (String) t.getTransferData(DataFlavor.stringFlavor);
				window.getTxtCodigo().replaceRange(data, selecaoInicio, selecaoFinal);
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	}

	public void salvar(){
		document.save();
	}
	
	public void salvarComo(){
		
	}
	
	public void abrir(){
		document.open();
		window.getTxtCodigo().setText(document.getNewFileContent());
	}

	public void novo() {
		document.novo();
		window.getTxtCodigo().setText(null);
		window.setNameArquivo("");
	}

	public Document getDocument() {
		return document;
	}

	public void setDocument(Document document) {
		this.document = document;
	}

	public void sair() {
		salvar();
		window.setVisible(false);
	}
	
	

}
